﻿Imports System.Data.SqlClient

Public Class adm_addcolumnnew
    Inherits System.Web.UI.Page
    Private sSQL As String
    Private objrd As SqlDataReader = Nothing
    Private objfind As New GetData
    Private ps As New PreparedStatement(sSQL)
    Dim strErr As String = ""

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Session("admin_ID") <> Nothing And Session("admin_ID") <> "") Then

            If (Request("subcolumnnews") <> Nothing And Request("subcolumnnews") <> "") Then
                Dim title, content, ndate, pId As String
                Dim newsId, fileFullName, fileName, photoPath As String
                title = strTitle.Text
                content = Request("c_content")
                ndate = Request("dojo_date")
                result.InnerHtml = ""
                If title = "" Then

                    result.InnerHtml = "<center><b><font color=""#FF0000""> Please enter your News title</font></b></center>"
                    Exit Sub
                End If
                If content = "" Then
                    result.InnerHtml = "<center><b><font color=""#FF0000""> Please enter your News content</font></b></center>"
                    Exit Sub
                End If
                If ndate = "" Then
                    result.InnerHtml = "<center><b><font color=""#FF0000""> Please choose your News date</font></b></center>"
                    Exit Sub
                End If
                If column.SelectedValue = "" Then
                    result.InnerHtml = "<center><b><font color=""#FF0000""> Please choose your News column</font></b></center>"
                    Exit Sub
                End If
                newsId = System.Guid.NewGuid().ToString()
                sSQL = "insert into Column_New (New_ID,title,create_date,Column_ID,context,news_date)"
                sSQL = sSQL & " values (@newsId,@title,@createDate,@column,@context,@newsDate)"
                ps.addParameter("newsId", newsId)
                ps.addParameter("title", title)
                ps.addParameter("createDate", Now)
                ps.addParameter("column", column.SelectedValue)
                ps.addParameter("context", content)
                ps.addParameter("newsDate", ndate)
                ps.strSql = sSQL
                objfind.es(ps, strErr)
                If (image.PostedFile.FileName <> Nothing) Then
                    pId = System.Guid.NewGuid().ToString()
                    fileFullName = image.PostedFile.FileName
                    fileName = fileFullName.Substring(fileFullName.LastIndexOf(".") + 1)
                    System.IO.Directory.CreateDirectory(Server.MapPath("../../") & "pic/news/" & newsId)
                    image.PostedFile.SaveAs(Server.MapPath("../../") & "pic/news/" & newsId & "/" & pId & "." & fileName)
                    photoPath = "/pic/news/" & newsId & "/" & pId & "." & fileName
                    ps.strSql = "update Column_New set photo_path = @photoPath where New_ID=@newsId"
                    ps.addParameter("photoPath", photoPath)
                    objfind.es(ps, strErr)
                    Me.news_id.Value = newsId
                    imageDiv.InnerHtml = "<img src=""" & photoPath & """  />"
                End If
                If strErr = Nothing Or strErr = "" Then
                    result.InnerHtml = ""
                    result.InnerHtml = "<center><b><font color=""#FF0000""> Add News Success！</font></b></center>"
                    strTitle.Text = ""
                    columnnews.InnerText = ""
                    column.SelectedIndex = 0
                Else
                    result.InnerHtml = ""
                    result.InnerHtml = "<center><b><font color=""#FF0000"">Error！</font></b><p>" & strErr & "</p></center>"
                End If
                Exit Sub
            End If
            column.Items.Add(New ListItem("-Column-", ""))
                sSQL = "select * from Columns"
            ps.strSql = sSQL
            Dim conn As SqlConnection = objfind.getConn()
            objrd = objfind.dr(ps, strErr, conn, objrd)

                While (objrd.Read())
                    column.Items.Add(New ListItem(
                     objrd("Column_Name").ToString(), objrd("Column_ID").ToString()))
                End While
            objfind.closeConn(conn)
        Else
            Response.Redirect("../adm_login.aspx")
        End If

    End Sub

   
    Protected Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        Response.Redirect("col_default.aspx")
    End Sub
End Class
